package Sql;

import Table.HeadUnit;
import Table.TableInfoCenter;
import Table.getAllInfo;
import java.sql.Date;
import java.sql.ResultSet;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;


public class User implements getAllInfo{
	
	
	private String uid;
	private String uname;
	private String usex;
	private String password;
	private int level;
	private String levelname;
	private Date lastlogin;
	private String address;  
	private String email;
	private String tel;
	private String others;
	
	
	public User(String uid, String uname, String usex, String password,
			int level, String levelname, String address,
			String email, String tel, String others) {
		super();
		this.uid = uid;
		this.uname = uname;
		this.usex = usex;
		this.password = password;
		this.level = level;
		this.levelname = levelname;
		this.address = address;
		this.email = email;
		this.tel = tel;
		this.others = others;
        try {
            Mysql mysql = new Mysql();
            mysql.executeUpdate("insert into user(uid,uname,usex,password,level) values('"+uid+"','"+uname+"','"+usex+"','"+password+"','"+level+"')");
            mysql.executeUpdate("insert into useinfo(uid,address,email,tel,others) values('"+uid+"','"+address+"','"+email+"','"+tel+"','"+others+"')");
            mysql.close();
        } catch (Exception ex) {        }

	}
	
	public User(String uid, String uname, String usex, String password,
			int level) {
		super();
		this.uid = uid;
		this.uname = uname;
		this.usex = usex;
		this.password = password;
		this.level = level;
                        try {
            Mysql mysql = new Mysql();
            mysql.executeUpdate("insert into user(uid,uname,usex,password,level) values('"+uid+"','"+uname+"','"+usex+"','"+password+"','"+level+"')");
            mysql.close();
        } catch (Exception ex) {        }
	}
	
	public User(){}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getUsex() {
		return usex;
	}

	public void setUsex(String usex) {
		this.usex = usex;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public String getLevelname() {
		return levelname;
	}

	public void setLevelname(String levelname) {
		this.levelname = levelname;
	}

	public Date getLastlogin() {
		return lastlogin;
	}

	public void setLastlogin(Date lastlogin) {
		this.lastlogin = lastlogin;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getOthers() {
		return others;
	}

	public void setOthers(String others) {
		this.others = others;
	}



	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((uid == null) ? 0 : uid.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (uid == null) {
			if (other.uid != null)
				return false;
		} else if (!uid.equals(other.uid))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "User [uid=" + uid + ", uname=" + uname + ", usex=" + usex
				+ ", password=" + password + ", level=" + level
				+ ", levelname=" + levelname + ", lastlogin=" + lastlogin
				+ ", address=" + address + ", email=" + email + ", tel=" + tel
				+ ", others=" + others + "]\n";
	};



        public TableInfoCenter getAllInfo() throws Exception {
            Mysql mysql = new Mysql();
            List<Object[]> al = new Vector<Object[]>();
		ResultSet rs = mysql.executeQuery("select * from view_user ");

		while(rs.next()){
			Object[] temp = {
                            rs.getString("uid"),
                            rs.getString("uname"),
                            rs.getString("usex"),
                            rs.getString("password"),
                            rs.getInt("level"),
                            rs.getString("levelname"),
                            rs.getDate("lastlogin"),
                            rs.getString("address"),
                            rs.getString("email"),
                            rs.getString("tel"),
                            rs.getString("others")
                        };

			al.add(temp);

		}

                HeadUnit[] alh = {
                    new HeadUnit("ID"),
                    new HeadUnit("名字"),
                    new HeadUnit("性别"),
                    new HeadUnit("密码"),
                    new HeadUnit("级别"),
                    new HeadUnit("级别名称"),
                    new HeadUnit("上次登录"),
                    new HeadUnit("地址"),
                    new HeadUnit("E-mail"),
                    new HeadUnit("电话"),
                    new HeadUnit("备注")
                };

                alh[2].setNeedComboBox(true);

                TableInfoCenter TIC = new TableInfoCenter();
                TIC.ColumnInfo = alh;
                TIC.InfoSet = al;
                mysql.close();
		return TIC;
        }


        public int getTableType() {
            return TableType.USER;
        }

        //删除指定用户
        public void delUser(String uid){
            try {
                Mysql mysql = new Mysql();
                mysql.executeUpdate("delete from user where uid = '"+uid+"'");
                mysql.executeUpdate("delete from userinfo where uid = '"+uid+"'");
                mysql.close();
            } catch (Exception ex) {}
        }

//        //更新用户信息
//        public void update(){
//            try {
//                Mysql mysql = new Mysql();
//                mysql.executeUpdate("update user set uname = '"+this.uname+"',usex = '"+this.usex+"', password='"+this.password+"', level = '"+this.level+"' where uid = '"+this.uid+"'");
//                mysql.executeUpdate("update userinfo set address = '"+this.address+"', email = '"+this.email+"', tel = '"+this.tel+"', others = '"+this.others+"' where uid = '"+this.uid+"'");
//                mysql.close();
//            } catch (Exception ex) { }
//        }



	
	

}
